package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	"time"
)

type name struct {
	Name string
}

func main() {
	mysql()
}

func mysql() {
	db, err := gorm.Open("mysql", "root:972561325@tcp(localhost:3306)/distributed?charset=utf8&parseTime=True&loc=Local")
	db.LogMode(true)
	// Error
	if err != nil {
		panic(err)
	}
	//设置连接池
	//空闲
	db.DB().SetMaxIdleConns(20)
	//打开
	db.DB().SetMaxOpenConns(100)
	//超时
	db.DB().SetConnMaxLifetime(time.Second * 30)
	go addMysql(db)

	time.Sleep(10 * time.Second)
}

func addMysql(db *gorm.DB) {
	u := name{Name: "aaa"}
	err := db.Create(&u).Error
	if err != nil {
		fmt.Println(err)
	}
}
